package com.yusvn.scm.goods.jiandang.controller;

import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.ext.cors.EnableCORS;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Record;
import com.yusvn.scm.common.BaseController;
import com.yusvn.scm.common.kit.RetKit;
import com.yusvn.scm.goods.jiandang.service.ScmJiandangJdfhService;


/**
 * 建档分货
 * @author liugz
 *
 */
@EnableCORS
public class ScmJiandangJdfhController extends BaseController {
	
	@Inject
	ScmJiandangJdfhService srv;
	
	public void index() {
		renderJson();
	}

	// 获取某商品所有门店零售价
	public void getMdlsj() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			List<Record> recList = srv.getMdlsj(kv);
			renderJson(RetKit.ok().setData( recList ) );
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("查询异常："+e.getMessage()));
		}
	}
	
	//表1 获取需分货商品
	public void getFhsp() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			List<Record> recList = srv.getFhsp(kv);
			renderJson(RetKit.ok().setData( recList ) );
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("查询异常："+e.getMessage()));
		}
	}

	//表1 引入哪天创建的商品
	public void yrFhsp() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			renderJson(srv.yrFhsp(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("异常："+e.getMessage()));
		}
	}
	
	//档案 删除商品条码
	public void delSku() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());

		try {
			renderJson(srv.delSku(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("删除异常："+e.getMessage()));
		}
	}
	
	//档案 增加商品颜色
	public void addSpys() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			renderJson(srv.addSpys(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("保存异常："+e.getMessage()));
		}
	}
	
	//档案 更新商品门店零售价格
	public void gxMdlsj() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			renderJson(srv.gxMdlsj(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("更新价格异常："+e.getMessage()));
		}
	}
	
	//表1 新增商品到分货商品中间表
	public void addFhsp() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		
		try {
			renderJson(srv.addFhsp(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("保存异常："+e.getMessage()));
		}
	}

	//表1 删除分货商品
	public void delFhsp() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());

		try {
			renderJson(srv.delFhsp(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("删除异常："+e.getMessage()));
		}
	}

	//表1 取消采入--按商品清除分货结果
	public void delFhjgsp() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());

		try {
			renderJson(srv.delFhjgsp(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("删除异常："+e.getMessage()));
		}
	}
	
	//表5 根据参数删除分货结果明细 （type=1按店款色，type=2按店条码，type=3按店款）
	public void delFhjgDt() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());

		try {
			renderJson(srv.delFhjgDt(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("删除异常："+e.getMessage()));
		}
	}
	
	//表1 获取状态
	public void getFhzt() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			List<Record> recList = srv.getFhzt(kv);
			renderJson(RetKit.ok().setData( recList ) );
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("查询异常："+e.getMessage()));
		}
	}
	
	//表3 获取本次采入结果（色码明细）
	public void getCrslSm() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			List<Record> recList = srv.getCrslSm(kv);
			renderJson(RetKit.ok().setData( recList ) );
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("查询异常："+e.getMessage()));
		}
		
	}
	
	//表3 修改采入数量
	public void updateCrsl() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			renderJson(srv.updateCrsl(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("异常："+e.getMessage()));
		}
	}
	
	//表3 修改采入发货价
	public void updateCrfhj() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			renderJson(srv.updateCrfhj(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("异常："+e.getMessage()));
		}
	}
	
	//表3 修改采入整手数
	public void updateCrzss() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			renderJson(srv.updateCrzss(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("异常："+e.getMessage()));
		}
	}
	
	//表4 获取采入结果（店汇总）
	public void getCrslMd() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			List<Record> recList = srv.getCrslMd(kv);
			renderJson(RetKit.ok().setData( recList ) );
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("查询异常："+e.getMessage()));
		}
	}

	//表4 删除分货结果--按门店
	public void delFhjgmd() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());

		try {
			renderJson(srv.delFhjgmd(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("删除异常："+e.getMessage()));
		}
	}
	
	//表4 删除分货结果 -清空所有
	public void delFhjg() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());

		try {
			renderJson(srv.delFhjg(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("删除异常："+e.getMessage()));
		}
	}

	//表4 复制粘贴单据分货结果
	public void copyFhjg() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());

		try {
			renderJson(srv.copyFhjg(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("删除异常："+e.getMessage()));
		}
	}

	//表4 生成单据
	public void scdj() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());

		try {
			renderJson(srv.scdj(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("删除异常："+e.getMessage()));
		}
	}
	
	//表4 修改手工单号（流水号）
	public void updateSgdh() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			renderJson(srv.updateSgdh(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("异常："+e.getMessage()));
		}
	}
	
	//表4 修改整单备注
	public void updateZdbz() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			renderJson(srv.updateZdbz(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("异常："+e.getMessage()));
		}
	}
	
	//表4 修改整单抹零
	public void updateMl() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			renderJson(srv.updateMl(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("异常："+e.getMessage()));
		}
	}
	
	//表4 修改修改业务员采购员
	public void updateYwy() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			renderJson(srv.updateYwy(kv));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("异常："+e.getMessage()));
		}
	}
	
	
	
	//表5 查看采入明细（某店所有款）
	public void getCrmx() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			List<Record> recList = srv.getCrmx(kv);
			renderJson(RetKit.ok().setData( recList ) );
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("查询异常："+e.getMessage()));
		}
		
	}
	
	//表4 获取业务员采购员
	public void getYwy() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			List<Record> recList = srv.getYwy(kv);
			renderJson(RetKit.ok().setData( recList ) );
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("查询异常："+e.getMessage()));
		}
	}

	/**
	 * 供应商查询商品
	 */
	public void getSpByGys(){
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		try {
			List<Record> recList = srv.getSpByGys(kv);
			renderJson(RetKit.ok().setData( recList ) );
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("查询异常："+e.getMessage()));
		}

	}

	/**
	 * 新增选择的商品
	 */
	public void qrxzsp(){
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());

		renderJson(srv.qrxzsp(kv));
	}
	
	/**
	 * 新增行
	 */
	public void xZh(){
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());

		renderJson(srv.xZh(kv));
	}

	/**
	 * 查询已经存在的商品
	 */
	public void selectCzsp(){

		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());

		renderJson(srv.selectCzsp(kv));
	}

	/**
	 *获取某个商品的颜色尺码
	 */
	public void selectSpyscm(){
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());

		renderJson(srv.selectSpyscm(kv));
	}

	/**
	 * 新增或修改单店录入明细
	 */
	public void insertDdlrmx(){
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		renderJson(srv.insertDdlrmx(kv));
	}

	/**
	 *	单店录入删除根据spid
	 */
	public void deleteDdlrBySpid(){
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		renderJson(srv.deleteDdlrBySpid(kv));
	}
	
	/**
	 * 单店删除全部商品
	 */
	public void deleteDdlrall(){
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		renderJson(srv.deleteDdlrall(kv));
	}

	/**
	 * 单店录入保存
	 */
	public void baoCunDdlr(){
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		kv.set("userCode", getLoginErpUsercode());
		renderJson(srv.baoCunDdlr(kv));
	}
	

	/**
	 * 单据编辑查询
	 */
	public void selectYinruDdlr(){
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		kv.set("userCode", getLoginErpUsercode());
		renderJson(srv.selectYinruDdlr(kv));
	}

	/**
	 * 确认引入
	 */
	public void qrYinruBymdid(){
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		kv.set("userCode", getLoginErpUsercode());
		renderJson(srv.qrYinruBymdid(kv));
	}
}
